/// -----------------------------------------------------------------------------------------------------------
/// Module      :  QueueStorageAdapterBindingElement.cs
/// Description :  Provides a base class for the configuration elements.
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceModel.Channels;
using System.Configuration;
using System.Globalization;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace BizTalkFactory.Adapters.AzureStorage
{
    public class AzureStorageAdapterBindingElement : StandardBindingElement
    {
        private ConfigurationPropertyCollection properties;

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the QueueStorageAdapterBindingElement class
        /// </summary>
        public AzureStorageAdapterBindingElement()
            : base(null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the QueueStorageAdapterBindingElement class with a configuration name
        /// </summary>
        public AzureStorageAdapterBindingElement(string configurationName)
            : base(configurationName)
        {
        }

        #endregion Constructors

        #region Custom Generated Properties

        // General Properties
        
        // Inbound Properties

        [Microsoft.ServiceModel.Channels.BindingPropertyDirection(true,false)]
        [System.ComponentModel.Category("Inbound Properties")]
        [System.Configuration.ConfigurationProperty("pollingIntervalInSeconds", DefaultValue = 30)]
        public int PollingIntervalInSeconds
        {
            get
            {
                return ((int)(base["pollingIntervalInSeconds"]));
            }
            set
            {
                base["pollingIntervalInSeconds"] = value;
            }
        }

        [Microsoft.ServiceModel.Channels.BindingPropertyDirection(true, false)]
        [System.ComponentModel.Category("Inbound Properties")]
        [System.Configuration.ConfigurationProperty("pollingStorage", DefaultValue = StorageType.Unspecified)]
        public StorageType PollingStorage
        {
            get
            {
                return ((StorageType)(base["pollingStorage"]));
            }
            set
            {
                base["pollingStorage"] = value;
            }
        }

        [Microsoft.ServiceModel.Channels.BindingPropertyDirection(true, false)]
        [System.ComponentModel.Category("Inbound Properties")]
        [System.Configuration.ConfigurationProperty("blobMask", DefaultValue = "*.*")]
        public String BlobMask 
        { 
            get
            {
                return (String)(base["blobMask"]);   
            }
            
            set
            {
                base["blobMask"] = value;
            }    
               
        }

        // Local Storage Emulator Connection String Parameters

        [System.ComponentModel.Category("Development Storage")]
        [System.Configuration.ConfigurationProperty("developmentStorageProxyUri", DefaultValue = "")]
        public string DevelopmentStorageProxyUri
        {
            get
            {
                return ((string)(base["developmentStorageProxyUri"]));
            }
            set
            {
                base["developmentStorageProxyUri"] = value;
            }
        }

        // Azure Storage Connection String Parameters

        [System.ComponentModel.Category("Storage Account Information")]
        [System.Configuration.ConfigurationProperty("accountKey", DefaultValue = AzureStorageAdapterConnectionUri.DevelopmentStorageAccountkey)]
        public String AccountKey
        {
            get
            {
                return ((string)(base["accountKey"]));
            }
            set
            {
                base["accountKey"] = value;
            }
        }

        [System.ComponentModel.Category("Storage Endpoints")]
        [System.Configuration.ConfigurationProperty("defaultEndpointsProtocol", DefaultValue = "https")]
        public String DefaultEndpointsProtocol
        {
            get
            {
                return ((string)(base["defaultEndpointsProtocol"]));
            }
            set
            {
                base["defaultEndpointsProtocol"] = value;
            }
        }

        [System.ComponentModel.Category("Storage Endpoints")]
        [System.Configuration.ConfigurationProperty("blobEndpoint", DefaultValue = "")]
        public String BlobEndpoint
        {
            get
            {
                return ((string)(base["blobEndpoint"]));
            }
            set
            {
                base["blobEndpoint"] = value;
            }
        }

        [System.ComponentModel.Category("Storage Endpoints")]
        [System.Configuration.ConfigurationProperty("queueEndpoint", DefaultValue = "")]
        public String QueueEndpoint
        {
            get
            {
                return ((string)(base["queueEndpoint"]));
            }
            set
            {
                base["queueEndpoint"] = value;
            }
        }

        [System.ComponentModel.Category("Storage Endpoints")]
        [System.Configuration.ConfigurationProperty("tableEndpoint", DefaultValue = "")]
        public String TableEndpoint
        {
            get
            {
                return ((string)(base["tableEndpoint"]));
            }
            set
            {
                base["tableEndpoint"] = value;
            }
        }

        [System.ComponentModel.Category("Storage Endpoints")]
        [System.Configuration.ConfigurationProperty("SharedAccessSignature")]
        public String SharedAccessSignature
        {
            get
            {
                return ((string)(base["sharedAccessSignature"]));
            }
            set
            {
                base["sharedAccessSignature"] = value;
            }
        }

        #endregion Custom Generated Properties

        #region Protected Properties

        /// <summary>
        /// Gets the type of the BindingElement
        /// </summary>
        protected override Type BindingElementType
        {
            get
            {
                return typeof(AzureStorageAdapterBinding);
            }
        }

        #endregion Protected Properties

        #region StandardBindingElement Members

        /// <summary>
        /// Initializes the binding with the configuration properties
        /// </summary>
        protected override void InitializeFrom(Binding binding)
        {
            base.InitializeFrom(binding);
            AzureStorageAdapterBinding adapterBinding = (AzureStorageAdapterBinding)binding;

            this["pollingIntervalInSeconds"] = adapterBinding.PollingIntervalInSeconds;
            this["pollingStorage"] = adapterBinding.PollingStorage;
            this["blobMask"] = adapterBinding.BlobMask;

            this["developmentStorageProxyUri"] = adapterBinding.DevelopmentStorageProxyUri;
            this["accountKey"] = adapterBinding.AccountKey;
            this["defaultEndpointsProtocol"] = adapterBinding.DefaultEndpointsProtocol;
            this["blobEndpoint"] = adapterBinding.BlobEndpoint;
            this["queueEndpoint"] = adapterBinding.QueueEndpoint;
            this["tableEndpoint"] = adapterBinding.TableEndpoint;
            this["sharedAccessSignature"] = adapterBinding.SharedAccessSignature;
        }

        /// <summary>
        /// Applies the configuration
        /// </summary>
        protected override void OnApplyConfiguration(Binding binding)
        {
            if (binding == null)
                throw new ArgumentNullException("binding");

            AzureStorageAdapterBinding adapterBinding = (AzureStorageAdapterBinding)binding;

            adapterBinding.PollingIntervalInSeconds = (int)(this["pollingIntervalInSeconds"]);
            adapterBinding.PollingStorage = (StorageType)(this["pollingStorage"]);
            adapterBinding.BlobMask = (String)(this["blobMask"]);

            adapterBinding.DevelopmentStorageProxyUri = (System.String) this["developmentStorageProxyUri"];

            adapterBinding.AccountKey = (System.String)this["accountKey"];
            adapterBinding.DefaultEndpointsProtocol = (System.String)this["defaultEndpointsProtocol"];
            adapterBinding.BlobEndpoint = (System.String)this["blobEndpoint"];
            adapterBinding.QueueEndpoint = (System.String)this["queueEndpoint"];
            adapterBinding.TableEndpoint = (System.String)this["tableEndpoint"];
            adapterBinding.SharedAccessSignature = (System.String)this["sharedAccessSignature"];
        }

        /// <summary>
        /// Returns a collection of the configuration properties
        /// </summary>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                if (this.properties == null)
                {
                    ConfigurationPropertyCollection configProperties = base.Properties;

                    configProperties.Add(new ConfigurationProperty("pollingIntervalInSeconds", typeof(System.Int32), 30, null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("pollingStorage", typeof(StorageType), StorageType.Unspecified, null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("blobMask",typeof(String),String.Empty,null,null,ConfigurationPropertyOptions.None));
                    
                    configProperties.Add(new ConfigurationProperty("developmentStorageProxyUri", typeof(System.String), String.Empty, null, null, ConfigurationPropertyOptions.None));
                    
                    configProperties.Add(new ConfigurationProperty("accountKey", typeof(System.String), AzureStorageAdapterConnectionUri.DevelopmentStorageAccountkey, null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("defaultEndpointsProtocol", typeof(System.String), "https", null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("blobEndpoint", typeof(System.String), String.Empty, null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("queueEndpoint", typeof(System.String), String.Empty, null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("tableEndpoint", typeof(System.String), String.Empty, null, null, ConfigurationPropertyOptions.None));
                    configProperties.Add(new ConfigurationProperty("sharedAccessSignature", typeof(System.String), String.Empty, null, null, ConfigurationPropertyOptions.None));

                    this.properties = configProperties;
                }

                return this.properties;
            }
        }

        #endregion StandardBindingElement Members
    }
}
